/**
* <p> title ： FlashData.java</p>
* <p> package ： com.akeil.flash.model</p>
* <p> description ：TODO</p>
* <p> copyright ： 哈尔滨工业大学(C) 2019</p>
* <p> company ： 哈尔滨工业大学AS&MT</p>
* <p> author ： 金雪松 jinxuesong@163.com</p>
* <p> date ： 2019年12月26日 下午8:46:31</p>
* <p> version ： v1.0 </p>
*
* Modification  History:
* Date         Author        Version        Discription
* -----------------------------------------------------------------------------------
* 2019年12月26日       金雪松          1.0             1.0
* Why & What is modified: <修改原因描述>
*/
package com.akeil.flash.model;

import java.util.Observable;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

import com.akeil.flash.action.StopTestAction;
import com.akeil.flash.config.EDeviceType;
import com.akeil.flash.config.IConstantsConfig;
import com.akeil.flash.dataprocessing.IConstantProcessor;
import com.akeil.flash.event.DeviceInfoNotify;
import com.akeil.flash.event.ENotifyType;
import com.akeil.flash.event.FlashPointNotify;
import com.akeil.flash.event.INotifyEvent;
import com.akeil.flash.event.StatusBarNotify;

import flash.Application;


/**
 * <p> classname ： FlashData</p>
 * <p> description ：TODO</p>
 * <p> author ： 金雪松 jinxuesong@163.com</p>
 * <p> date ： 2019年12月26日 下午8:46:31</p>
 */
public class FlashData extends Observable{
	private double temperature;
	private double atm;
	private double desktopTemperature;
	private double coupleTemperature;
	
	private double flashpoint;
	private double delta;
	private EDeviceStatusType status;
	private int watchDogCount;
	//仪器的工作状态信息
	private StatusBarNotify statusBarNotify = new StatusBarNotify();
	private DeviceInfoNotify deviceInfoNotify = new DeviceInfoNotify();
	private FlashPointNotify flashPointNotify = new FlashPointNotify();
	private boolean working = false;
	private boolean received = false;
	private boolean warning = false;

	public double getTemperature() {
		return temperature;
	}

	public void setTemperature(double temperature) {
		this.temperature = temperature;
		deviceInfoNotify.setNotifyType(ENotifyType.TEMPERATURE_INFO);
		deviceInfoNotify.setValue(temperature);
		measurementsChanged(deviceInfoNotify);
	}
	
	public double getAtm() {
		return atm;
	}

	public void setAtm(double atm) {
		this.atm = atm;
		deviceInfoNotify.setNotifyType(ENotifyType.ATM_INFO);
		deviceInfoNotify.setValue(atm);
		measurementsChanged(deviceInfoNotify);
		
		flashPointNotify.setNotifyType(ENotifyType.ATM_INFO);
		flashPointNotify.setValue(atm);
		measurementsChanged(flashPointNotify);
		
		deviceInfoNotify.setNotifyType(ENotifyType.SERIAL_INFO);
		deviceInfoNotify.setValue(watchDogCount);
		measurementsChanged(deviceInfoNotify);
	}
	
	public void setDelta(double d){
		this.delta = d;
		
		flashPointNotify.setNotifyType(ENotifyType.DELTA);
		flashPointNotify.setValue(delta);
		measurementsChanged(flashPointNotify);
	}

	public double getDesktopTemperature() {
		return desktopTemperature;
	}

	public void setDesktopTemperature(double desktopTemperature) {
		this.desktopTemperature = desktopTemperature;
		deviceInfoNotify.setNotifyType(ENotifyType.DESKTOP_INFO);
		deviceInfoNotify.setValue(desktopTemperature);
		measurementsChanged(deviceInfoNotify);
	}

	public double getCoupleTemperature() {
		return coupleTemperature;
	}

	public void setCoupleTemperature(double coupleTemperature) {
		this.coupleTemperature = coupleTemperature;
		deviceInfoNotify.setNotifyType(ENotifyType.COUPLE_INFO);
		deviceInfoNotify.setValue(coupleTemperature);
		measurementsChanged(deviceInfoNotify);
	}

	public double getFlashpoint() {
		return flashpoint;
	}

	public void setFlashpoint(double flashpoint) {
		if(!received){
			received = true;
			this.flashpoint = flashpoint;
			flashPointNotify.setNotifyType(ENotifyType.FLASHPOINT_INFO);
			flashPointNotify.setValue(flashpoint);
			measurementsChanged(flashPointNotify);
		}
	}
	
	public EDeviceStatusType getStatus() {
		return status;
	}

	public void setStatus(EDeviceStatusType status) {
		this.status = status;
		statusBarNotify.setNotifyType(ENotifyType.DEVICE_INFO);
		switch(status){
		case READY:
			statusBarNotify.setStatusContent("准备试验");
			//清除警告状态位
			warning = false;
			break;
		
		case FINISH:
			statusBarNotify.setStatusContent("试验结束");
			working = false;
			break;

		case ANASYSIS:
			statusBarNotify.setStatusContent("正在试验");
			Application.resultView.startTest();
			working = true;
			break;
		}
		
		measurementsChanged(statusBarNotify);
	}
	
	public void setStatus(int status) {
		switch(status){
		case 1:// 预置温度偏低
			if(!warning){
				warning = true;
				Display display = Display.getDefault();
				display.asyncExec(new Runnable(){

					@Override
					public void run() {
						// TODO 自动生成的方法存根
						MessageDialog.openWarning(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "警告", "连续测试温度超过预置温度和滞后温度之和仍没有闪火现象，预置温度偏低。请提高预置温度重新试验。");
					}
				});
			}
			break;
			
		case 2:// 预置温度偏高
			if(!warning){
				warning = true;
				Display display = Display.getDefault();
				display.asyncExec(new Runnable(){

					@Override
					public void run() {
						// TODO 自动生成的方法存根
						if(IConstantsConfig.deviceType == EDeviceType.MANUAL){
							MessageDialog.openWarning(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "警告", "第一次开盖点火时，有闪火现象被测出， 该值为无效值。设置预置温度偏高，请降低预置温度重新试验。");
						}else{
							MessageDialog.openWarning(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "警告", "第一次扫描点火时，有闪火现象被测出， 该值为无效值。设置预置温度偏高，请降低预置温度重新试验。");
						}
						
						
					}
				});
			}
			break;
			
		case 3:// 底座温度高
			if(!warning){
				warning = true;
				Display display = Display.getDefault();
				display.asyncExec(new Runnable(){

					@Override
					public void run() {
						// TODO 自动生成的方法存根
						MessageDialog.openWarning(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "警告", "底座温度过高，请核查操作是否正确后再做试验。正在降温，请稍后...");
					}
				});
			}
			break;
		}
		//snow 2020-03-05 增加
		Application.processor.sendCommand((byte)IConstantProcessor.CancelFX);
		Application.flashData.setStatus(EDeviceStatusType.FINISH);
		Application.flashData.setReceived(false);
		Application.resultView.stopTest();
		statusBarNotify.setNotifyType(ENotifyType.DEVICE_INFO);
		measurementsChanged(statusBarNotify);
	}

	public void measurementsChanged(INotifyEvent event) {
		this.setChanged();
		this.notifyObservers(event);
	}
	
	public void refreshCommunication(){
		watchDogCount = 5;
	}

	public boolean isReceived() {
		return received;
	}

	public void setReceived(boolean received) {
		this.received = received;
	}

	public int getWatchDogCount() {
		return watchDogCount;
	}

	public void setWatchDogCount(int watchDogCount) {
		this.watchDogCount = watchDogCount;
	}

	public DeviceInfoNotify getDeviceInfoNotify() {
		return deviceInfoNotify;
	}

	public void setDeviceInfoNotify(DeviceInfoNotify deviceInfoNotify) {
		this.deviceInfoNotify = deviceInfoNotify;
	}

	public boolean isWorking() {
		return working;
	}

	public void setWorking(boolean working) {
		this.working = working;
	}
	
}
